package priv.pront.code.national.acwing.dp.linear.trangle;

import java.util.Scanner;

public class A1015_摘花生 {

    static int[][] g, dp;
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0 ; i < t; i++){
            int r = scanner.nextInt();
            int c = scanner.nextInt();
            g = new int[r + 1][c + 1];
            dp = new int[r + 1][c + 1];
            for(int j = 1; j <= r; j++){
                for(int k = 1; k <=c ;k++){
                    g[j][k] = scanner.nextInt();
                }
            }
            dp[1][1] = g[1][1];
            for(int h = 2; h <= c; h++){
                dp[1][h] = dp[1][h - 1] + g[1][h];
            }
            for(int h = 2; h <= r; h++){
                dp[h][1] = dp[h - 1][1] + g[h][1];
            }
            for(int y = 2; y <= r; y++){
                for(int u = 2; u <= c; u++){
                    dp[y][u] = Math.max(dp[y - 1][u], dp[y][u - 1]) + g[y][u];
                }
            }
            System.out.println(dp[r][c]);
        }
    }
}
